<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <title>Layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="lib/layui/css/layui.css"  media="all">
    <script type="text/javascript" src="lib/js/cookies.js"></script>
    <!-- 注意：如果你直接复制所有代码到本地，上述css路径需要改成你本地的 -->
</head>
<body>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<fieldset class="layui-elem-field layui-field-title">
    <legend>货验单新增</legend>
</fieldset>
<div class="layui-input-inline">
    <label><span style="color: red">*</span>供应商</label>
    <div class="layui-inline"><input type="text" readonly id="supplierName" class="layui-input inputText" value="" ></div>
    <div class="layui-inline"><input type="button" class="layui-btn layui-btn-sm" value="选择" onclick="show()"></div>
    <input type="hidden" id="supplierId" name="supplierId" value="">
</div>
<div class="layui-input-inline">
    <label><span style="color: red">*</span>采购单Id</label>
    <div class="layui-inline"><input type="text" readonly id="purchaseOrderCode" class="layui-input inputText" value="" ></div>
    <div class="layui-inline"><input type="button" class="layui-btn layui-btn-sm" value="选择" onclick="showPurchase()"></div>
    <input type="hidden" id="purchaseOrderId" name="purchaseOrderId" value="">
</div>

<table class="layui-hide" id="test" lay-filter="test"></table>
<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <!--        <button class="layui-btn layui-btn-sm" lay-event="insertPurchase">新增采购单</button>-->
        <button class="layui-btn layui-btn-sm" lay-event="tijiao">提交</button>
        <!--<button class="layui-btn layui-btn-sm" lay-event="isAll">验证是否全选</button>-->
    </div>
</script>
</body>
<script src="lib/jquery-3.4.1.min.js"></script>
<script src="lib/layui/layui.js" charset="utf-8"></script>
<script type="text/javascript">
    function showPurchase() {
        layui.use('layer',function () {
            var layer = layui.layer;
            layer.open({
                type: 2,
                area: ['600px','350px'],
                content: 'deliveryOrder_purchaseOrder.html'
            })
        });
    }
    var len = 0;
    var total = 0;
    function getPurchase(res) {
        $("#purchaseOrderCode").val(res[0].purchaseOrderCode);
        $("#purchaseOrderId").val(res[0].id)
        relod(res[0].id);
        total = res[0].purchaseOrderCost;
        console.log(total);
    }

    function show() {
        layui.use('layer',function () {
            var layer = layui.layer;
            layer.open({
                type: 2,
                area: ['600px','350px'],
                content: 'deliveryOrder_provider.html'
            })
        });
    }
    function get(res) {
        $("#supplierId").val(res[0].supplierId)
        $("#supplierName").val(res[0].supplierName)
    }
    function relod(re){
        layui.use('table', function(){
            var table = layui.table;
            table.reload('test',{
                page: {
                    curr: 1 //重新从第一页开始
                }, where: {
                    id: re
                },parseData:function (res) {
                    len = res.obj.length;
                    console.log(len)
                    return {
                        "code": res.code, //解析接口状态
                        "msg": res.msg, //解析提示文本
                        "count": res.obj.tolal, //解析数据长度
                        "data": res.obj //解析数据列表
                    };
                }
            })
        })
    }

    layui.use(['table','layer'], function(){
            var table = layui.table;
            var layer = layui.layer;
            //监听行工具事件
            table.render({
                elem: '#test'
                ,toolbar: '#toolbarDemo' //开启头部工具栏，并为其绑定左侧模板
                ,url:'htz/purchaseOrder/selDetail',
                parseData: function(res) { //res 即为原始返回的数据
                    return {
                        "code": res.code, //解析接口状态
                        "msg": res.msg, //解析提示文本
                        "count": res.obj.tolal, //解析数据长度
                        "data": res.obj //解析数据列表
                    };
                }
                ,cols: [[
                    {type: 'checkbox', fixed: 'left',width:40},
                    {field:'goodsName', title: '商品名称', sort: true}
                    ,{field:'quantityOfGoods',title: '购买数量', sort: true,edit:'text'}
                    ,{field:'purchasePrice', title: '购买单价(元)',width:140, sort: true}
                    // ,{field:'num1', title: '已到达数量', sort: true}
                    // ,{fixed: 'right', title:'操作', toolbar: '#barDemo'}
                ]],
                page: false
                ,where: {
                    id: 99999999
                }
            });

            //监听行工具事件
            table.on('tool(test)', function(obj){
                var data = obj.data;
                if(obj.event === 'del'){
                    append();
                    // var layer = layui.layer;
                    // layer.confirm('真的删除行么', function(index){
                    //     obj.del();
                    //     layer.close(index);
                    // });
                } else if(obj.event === 'edit'){
                    var layer = layui.layer;
                    layer.open({
                        type: 2,
                        area: ['900px','510px'],
                        content: 'delivery_detail1.html' //这里content是一个普通的String
                    });
                }
            });
        table.on('toolbar(test)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            var da = checkStatus.data;
            switch(obj.event){
                case 'tijiao' :
                    if($("#supplierId").val()!=''&&$("#supplierName").val()!=''){
                            if(da.length<=0){
                                layer.msg("请选择采购单或者选中商品")
                            }else {
                                if (da.length<len){
                                    layer.msg("请全部选中")
                                } else {
                                    var flag = false;
                                    for (var i = 0; i <da.length ; i++) {
                                        var reg = /^[1-9]\d*$/;
                                        if (!reg.test(da[i].quantityOfGoods)){
                                            flag = true;
                                        }
                                    }
                                    if (!flag){
                                        var token = getCookie("token");
                                        $.post('htz/user/token',{token,token},function (res) {
                                            if (res!=null){
                                                var userId = res.id;
                                                var purchaseUtil = {
                                                    list: da,
                                                    total : total,
                                                    userId: userId,
                                                    purchaseOrderCode: $("#purchaseOrderCode").val(),
                                                    purchaseId: $("#purchaseOrderId").val(),
                                                    supplierId: $("#supplierId").val()
                                                }
                                                $.ajax({
                                                    url: "pk/deliveryOrder/addDeliveryOrder",
                                                    data: JSON.stringify(purchaseUtil),
                                                    contentType: "application/json;charset=UTF-8",
                                                    type: "post",
                                                    dataType: "json",
                                                    success: function (res) {
                                                        if(res.obj>0){
                                                            layer.msg("货验单新增成功")
                                                            setTimeOut(function () {
                                                                window.location.href="deliveryOrder_add.html";
                                                            },1000)
                                                        }
                                                    }
                                                })
                                            }
                                        })
                                    } else {
                                        layer.msg("数量只能为正整数")
                                    }

                                }
                            }
                    }else {
                        layer.msg("请选择一个提供商")
                    }


                    break;
                // case 'auditPurchase':
                //     var data = checkStatus.data;
                //     if (data==''){
                //         layer.msg("请选择一个采购单")
                //     }else {
                //         layer.confirm('确认变为可审核',function (index) {
                //             var flag = false;
                //             console.log(data);
                //             for (var i = 0; i <data.length ; i++) {
                //                 if (data[i].status>1){
                //                     layer.msg("只有新录入的才能变为待审核")
                //                     flag = true;
                //                     break;
                //                 }
                //             }
                //             if (!flag){
                //                 $.ajax({
                //                     type: "post",
                //                     url: "htz/purchaseOrder/updAuditBylist",
                //                     data: JSON.stringify(data),
                //                     contentType: "application/json;charset=UTF-8",
                //                     dataType: "json",
                //                     success:function (data) {
                //                         if (data.obj>0){
                //                             layer.msg("修改成功");
                //                             relod();
                //                         }else {
                //                             layer.msg("修改失败")
                //                         }
                //                     }
                //                 })
                //             }
                //         });
                //     }
                //     break;
            }
        });
        })
</script>
</html>